package com.iuie.ns.frame.free.excel.beans;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.iuie.ns.frame.free.excel.enums.FreeWorkBookType;


/**
 * 导出复杂的Excel的工具
 * @author liujie
 * @date 2018年5月13日下午4:20:01
 */
public class FreeExcelWorkBook {
	
	private List<FreeExcelSheet> sheets;
	
	private FreeWorkBookType workBookType;
	
	private OutputStream outputStream;
	
	private FreeExcelExecutor executor;
	
	private FreeExcelWorkBook() {
		
	}

	public FreeExcelWorkBook newInstance() {
		return new FreeExcelWorkBook();
	}
	
	public FreeExcelWorkBook addSheet(FreeExcelSheet sheet) {
		if (sheets == null) {
			sheets = new ArrayList<>();
		}
		sheets.add(sheet);
		return this;
	}
	
	public FreeExcelWorkBook workBookType(FreeWorkBookType workBookType) {
		this.workBookType = workBookType;
		return this;
	}
	
	public FreeExcelWorkBook outputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
		return this;
	}
	
	public void export() throws IOException {
		// 获取执行器
		if (this.workBookType == null) {
			this.workBookType = FreeWorkBookType.XLSX;
		}
		if (this.workBookType == FreeWorkBookType.XLS) {
			this.executor = new FreeExcelXlsExecutor();
		}
		if (this.workBookType == FreeWorkBookType.XLSX) {
			this.executor = new FreeExcelXlsxExcutor();
		}
		// 具体执行
		if (this.executor != null) {
			this.executor.execute(this);
		}
		if (this.outputStream != null) {
			this.outputStream.flush();
			this.outputStream.close();
		}
	}

	public List<FreeExcelSheet> getSheets() {
		return sheets;
	}

	public FreeWorkBookType getWorkBookType() {
		return workBookType;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}
	
}
